package com.woniuxy.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.hc.entity.Paper;
import com.hc.entity.Shop;
import com.hc.utils.ResponseEntity;

import com.woniuxy.article.feign.ShopFeignClient;
import com.woniuxy.article.mapper.ArticleMapper;
import com.woniuxy.article.mapper.PaperMapper;
import com.woniuxy.article.service.ArticleService;

import com.woniuxy.article.utils.gaode.GaoDeMapUtil;
import com.woniuxy.article.utils.gaode.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 *@ClassName ArticleServiceImpl
 *@Description
 *@Author TJ
 *@Date 2022/10/20 9:33
 *@Version 1.0
 **/
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ShopFeignClient shopFeignClient;

    @Autowired
    GaoDeMapUtil gaoDeMapUtil;
    @Autowired
    PaperMapper paperMapper;
    @Autowired
    ArticleMapper articleMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public List<Shop> getByMessage(String message) {
        List<Shop> shops = null;
        ValueOperations opsForValue = redisTemplate.opsForValue();

        if(opsForValue.get(message) ==null){
            //通过模糊查询，查出多篇文章信息
            List<Paper> papers = articleMapper.getByMessage(message);

            //根据返回文章中的商品id，得到商家集合
            //定义集合shops，存放返回的商家信息
            shops = new ArrayList<>();

            //定义集合,存放商家的距离信息
            TreeMap<Long, Integer> sm = new TreeMap<>(new Comparator<Long>() {
                @Override
                public int compare(Long o1, Long o2) {
                    int num=(int) (o1 - o2);
                    return num;
                   //== 0 ? discount2 - discount1 : num;;
                }
            });

            //定义集合tm，存放文章的点赞和评论数，并进行排序
            TreeMap<Integer, Integer> tm = new TreeMap<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer discount1, Integer discount2) {
                    return discount2 - discount1;
                }
            });

            for (Paper paper : papers) {
                int i = paper.getLikenum() + paper.getUnlikenum() + paper.getDiscount();
                tm.put(i, paper.getId());
                ResponseEntity<Shop> shop = shopFeignClient.getByGoodsId(paper.getGoodsId());
                //计算距离
                //模拟初始化用户位置信息 四川省体育馆：104.065270,30.631324
                //查询到商家位置，调用高德地图工具类
                Result startLonLat = (gaoDeMapUtil.getLonLat("四川省体育馆"));

                //根据商家地址获取经纬度
                Result endLonLat = (gaoDeMapUtil.getLonLat(shop.getData().getAddress()));

                //根据经纬度算距离
                Result distance = gaoDeMapUtil.getDistance((String) startLonLat.getDatas(), (String) endLonLat.getDatas());
                sm.put((Long) distance.getDatas(), paper.getId());
            }

            //点赞排序
            Set<Map.Entry<Integer, Integer>> numSet = tm.entrySet();
            for (Map.Entry<Integer, Integer> entry : numSet) {
                Integer key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("文章点赞+评论数:" + key + "..." + "商品id:" + value);
                //经过排序后的文章，再去找商家
                ResponseEntity<Shop> numShop = shopFeignClient.getByGoodsId((Integer) value);
                Shop data = numShop.getData();
                shops.add(data);
            }
        /*//距离排序
        Set<Map.Entry<Long, Integer>> distanceSet = sm.entrySet();
        for (Map.Entry<Long, Integer> entry : distanceSet) {
            Long key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("用户和商家距离：" + key + "..." + "商品id：" + value);
            //经过排序后的文章，再去找商家
            ResponseEntity<Shop> distanceShop = shopFeignClient.getByGoodsId((Integer) value);
            //将店铺返回，距离
            Shop data = distanceShop.getData();
            shops.add(data);
        }*/
            redisTemplate.opsForValue().set(message,shops);
            return shops;
        }else {
            Object o = redisTemplate.opsForValue().get(message);
            return (List<Shop>) o;
        }
    }

    /**
     * 根据用户id获取用户的所有文章
     * @param id
     * @return
     */
    @Override
    public List<Paper> getPaperByUserId(int id) {
        List<Paper> paperMappers = paperMapper.selectAllByUserId(id);
        return paperMappers;
    }

    @Override
    public Paper getPaperByPaperId(int id) {
        Paper paper = paperMapper.selectByPaperId(id);
        return paper;
    }
}
 